/*
 * @lc app=leetcode id=72 lang=cpp
 *
 * [72] Edit Distance
 */
class Solution {
public:
    int minDistance(string word1, string word2) {

        int len1 = word1.size();
        int len2 = word2.size();

        vector<vector<int>> memo(len1+1, vector<int>(len2+1));

        for (int i=0; i<=len1; i++) {
            memo[i][0] = i;
        }

        for (int i=0; i<=len2; i++) {
            memo[0][i] = i;
        }

        for (int i=1; i<=len1; i++) {
            for (int j=1; j<=len2; j++) {
                if (word1[i-1] == word2[j-1]) {
                    memo[i][j] = std::min(std::min(memo[i-1][j]+1, memo[i][j-1]+1), memo[i-1][j-1]);
                } else {
                    memo[i][j] = std::min(std::min(memo[i-1][j]+1, memo[i][j-1]+1), memo[i-1][j-1]+1);
                }
            }
        }
        return memo[len1][len2];
    }
};

